﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Helpers;
using JustinAngel.UnitedNationsNews.Framework;
using JustinAngel.UnitedNationsNews.Framework.Helpers;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace JustinAngel.UnitedNationsNews.Pages.Photos
{
    public partial class PhotosPage : BasePage
    {
        public PhotosPage()
        {
            InitializeComponent();

            _cachedAppBar = this.ApplicationBar;
            IoC.Get<IMessenger>().Register<PhotosDetailsPaneVisiblityChangedMessage>(this,
                msg => DispatcherTimerHelper.On(TimeSpan.FromMilliseconds(200),
                    () => OnPhotosDetailsPaneVisiblityChanged(msg)));
        }

        private IApplicationBar _cachedAppBar = null;
        private void OnPhotosDetailsPaneVisiblityChanged(PhotosDetailsPaneVisiblityChangedMessage obj)
        {
            if (obj.Content)
            {
                this.ApplicationBar = _cachedAppBar;
            }
            else
            {
                _cachedAppBar = this.ApplicationBar;
                this.ApplicationBar = null;
            }

        }

        private void GestureListener_Tap(object sender, GestureEventArgs e)
        {
            RealVM.PhotoPageTap.Execute(null);
        }

        private void AppBarPreviousClick(object sender, EventArgs e)
        {
            RealVM.PreviousAppBarButtonClick.Execute(null);
        }

        private void AppBarNextClick(object sender, EventArgs e)
        {
            RealVM.NextAppBarButtonClick.Execute(null);

        }

        private void AppBarSaveClick(object sender, EventArgs e)
        {
            var pivotItem = (PivotItem)photosPivot.ItemContainerGenerator.ContainerFromIndex(photosPivot.SelectedIndex);
            var image = pivotItem.GetChild<Image>();
            RealVM.SavePictureLocally.Execute(image);
        }

        public PhotosViewModel RealVM
        {
            get { return this.ViewModel.As<PhotosViewModel>(); }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            var text = AddNavigatingText();

            if (Orientation == PageOrientation.PortraitDown || Orientation == PageOrientation.PortraitUp)
                text.Margin = new Thickness(0, 32, 0, 0);
            else if (Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight)
                text.Margin = new Thickness(32, 0, 0, 0);

            Dispatcher.BeginInvoke(() => DoBaseBackKey(e));
        }

        private void AppBarShareViaEmail(object sender, EventArgs e)
        {
            RealVM.ShareViaEmail.Execute(null);
        }

        private void AppBarShareOnTwitter(object sender, EventArgs e)
        {
            RealVM.ShareOnTwitter.Execute(null);
        }

        private void AppBarShareOnFacebook(object sender, EventArgs e)
        {
            RealVM.ShareOnFacebook.Execute(null);
        }


    }
}